package resource.pojo;

import java.io.Serializable;
import java.sql.Date;
import java.sql.ResultSet;

import models.util.MySqlDataAccessHelper;

public class Approvebuy implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;
	private Integer iDApproveBuy;
	private String title;
	private String content;
	private String iDEmployee;
	private String iDGroupApprove;
	private Date timecreate;
	private Integer iDStatus;
	private Integer iDTypeDoc;
	private Date timeApprove;
	private String comment;
	private String purpose;
	private Typedoc typeDoc;
	private GroupApprove groupApprove;
	private Employee employee;
	private Status status;
	private boolean checkAddReceiver=false;
	MySqlDataAccessHelper mysqlHelper = new MySqlDataAccessHelper();

	public MySqlDataAccessHelper getMysqlHelper() {
		return mysqlHelper;
	}

	public void setMysqlHelper(MySqlDataAccessHelper mysqlHelper) {
		this.mysqlHelper = mysqlHelper;
	}

	public Approvebuy() {
	}

	public Approvebuy(Integer iDApproveBuy, String title, String content,
			String iDEmployee, String iDGroupApprove, Date timecreate,
			Integer iDStatus, Integer iDTypeDoc, Date timeApprove,
			String comment,String purpose) {
		super();
		this.iDApproveBuy = iDApproveBuy;
		this.title = title;
		this.content = content;
		this.iDEmployee = iDEmployee;
		this.iDGroupApprove = iDGroupApprove;
		this.timecreate = timecreate;
		this.iDStatus = iDStatus;
		this.iDTypeDoc = iDTypeDoc;
		this.timeApprove = timeApprove;
		this.comment=comment;
		this.purpose = purpose;
	}

	public Integer getiDApproveBuy() {
		return iDApproveBuy;
	}

	public void setiDApproveBuy(Integer iDApproveBuy) {
		this.iDApproveBuy = iDApproveBuy;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getiDEmployee() {
		return iDEmployee;
	}

	public void setiDEmployee(String iDEmployee) {
		this.iDEmployee = iDEmployee;
	}

	public Date getTimecreate() {
		return timecreate;
	}

	public void setTimecreate(Date timecreate) {
		this.timecreate = timecreate;
	}

	public Integer getiDStatus() {
		return iDStatus;
	}

	public void setiDStatus(Integer iDStatus) {
		this.iDStatus = iDStatus;
	}


	public String getiDGroupApprove() {
		return iDGroupApprove;
	}

	public void setiDGroupApprove(String iDGroupApprove) {
		this.iDGroupApprove = iDGroupApprove;
	}

	public Integer getiDTypeDoc() {
		return iDTypeDoc;
	}

	public void setiDTypeDoc(Integer iDTypeDoc) {
		this.iDTypeDoc = iDTypeDoc;
	}

	public Date getTimeApprove() {
		return timeApprove;
	}

	public void setTimeApprove(Date timeApprove) {
		this.timeApprove = timeApprove;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getPurpose() {
		return purpose;
	}

	public void setPurpose(String purpose) {
		this.purpose = purpose;
	}

	public Typedoc getTypeDoc() {
		mysqlHelper.open();
		try {
			String sql = "select * from typedoc where IDTypeDoc="
					+ this.getiDTypeDoc();
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next()) {
				typeDoc = new Typedoc(rs.getInt("IDConfigStaticLang"));
				ConfigStaticLang configStaticLang = typeDoc
						.getConfigStaticLang();
				if (configStaticLang != null) {
					typeDoc.setiDTypeDoc(rs.getInt("IDTypeDoc"));
					typeDoc.setTypeName(configStaticLang.getNameByLang("EN"));
					typeDoc.setiDContentDoc(rs.getInt("IDContentDoc"));
					typeDoc.setiDTemplate(rs.getInt("IDTemplate"));
					typeDoc.setDescription(rs.getString("Description"));
				} else {
					typeDoc.setiDTypeDoc(rs.getInt("IDTypeDoc"));
					typeDoc.setTypeName(rs.getString("TypeName"));
					typeDoc.setiDContentDoc(rs.getInt("IDContentDoc"));
					typeDoc.setiDTemplate(rs.getInt("IDTemplate"));
					typeDoc.setDescription(rs.getString("Description"));
				}
			}
		} catch (Exception e) {
		}
		return typeDoc;
	}

	public void setTypeDoc(Typedoc typeDoc) {
		this.typeDoc = typeDoc;
	}

	public GroupApprove getGroupApprove() {
		mysqlHelper.open();
		try {
			String sql = "select * from groupapprove where IDGroupApprove='"
					+ this.getiDGroupApprove()+"'";
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next()) {
				groupApprove = new GroupApprove(rs.getString("IDGroupApprove"),
						rs.getBoolean("StatusUse"), rs.getString("GroupName"),
						rs.getString("Description"));
			}
			System.out.println("Group ");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			mysqlHelper.close();
		}
		return groupApprove;
	}

	public void setGroupApprove(GroupApprove groupApprove) {
		this.groupApprove = groupApprove;
	}

	public Employee getEmployee() {
		mysqlHelper.open();
		try {
			String sql = "select * from employee where IDEmployee='"
					+ this.iDEmployee+"'";
			ResultSet rs = mysqlHelper.executeQuery(sql);
			while (rs.next()) {
				employee = new Employee(rs.getString("IDEmployee"),
						rs.getString("UserName"), rs.getString("Password"),
						rs.getString("FullName"), rs.getString("Phone"),
						rs.getDate("Birthday"), rs.getString("Address"),
						rs.getString("Email"), rs.getString("Sex"),
						rs.getString("Position"), rs.getString("IDManager"),
						rs.getInt("IDDepartment"), rs.getInt("IDRole"),
						rs.getString("Description"));
			}
		} catch (Exception e) {

		} finally {
			mysqlHelper.close();
		}
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Status getStatus() {
		mysqlHelper.open();
		try {
			String sql = "select * from status where IDStatus="
					+ this.getiDStatus();
			ResultSet rs = mysqlHelper.executeQuery(sql);
			if (rs.next()) {

				status = new Status(rs.getInt("IDConfigStaticLang"));
				ConfigStaticLang configStaticLang = status
						.getConfigStaticLang();
				if (configStaticLang != null) {
					status.setiDStatus(rs.getInt("IDStatus"));
					status.setStatusName(configStaticLang.getNameByLang("EN"));
					status.setDescription(rs.getString("Description"));
				} else {
					status.setiDStatus(rs.getInt("IDStatus"));
					status.setStatusName(rs.getString("StatusName"));
					status.setDescription(rs.getString("Description"));
				}
			}
		} catch (Exception e) {
		} finally {
			mysqlHelper.close();
		}

		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((iDApproveBuy == null) ? 0 : iDApproveBuy.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Approvebuy other = (Approvebuy) obj;
		if (iDApproveBuy == null) {
			if (other.iDApproveBuy != null)
				return false;
		} else if (!iDApproveBuy.equals(other.iDApproveBuy))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Approvebuy [tile=" + title + "]";
	}

	public boolean isCheckAddReceiver() {
		return checkAddReceiver;
	}

	public void setCheckAddReceiver(boolean checkAddReceiver) {
		this.checkAddReceiver = checkAddReceiver;
	}
	
}
